<?php

/**
 * 文件说明： 点卡绑定以及会员绑定模块
 * ============================================================================
 * 版权所有 (C) 2010-2011 微凡工作室 | 微凡网络，并保留所有权利。
 * 网站地址: http://www.admin80.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 微凡工作室 | 微凡网络 $
 * $Encoding: UTF-8 $
 * $Id: MY_index.php  2011-3-20 上午02:31:49Z 微凡工作室 | 微凡网络 $
*/

defined('IN_PHPCMS') or exit('No permission resources.');

class MY_index extends index{
	
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * 绑定会员点卡，中断提示为以下情形：被他人绑定、过期未绑定的、账号密码不匹配的
	 * @author 微凡工作室 | 微凡网络
	 * @todo   鉴于绑定方式改变，此方法暂时废弃
	 */
	private function bind_member_card($user_model_info, $showtip = 0, $unset_uid = 0)
	{
		if (!is_array($user_model_info) || $this->memberinfo['modelid']!=10) return $user_model_info;
		if ($user_model_info['s_member_cardid'] && $user_model_info['s_member_cardpwd'] && module_exists('pointcard'))
		{
			$pc_db = pc_base::load_model('pointcard_model');
			$r = $pc_db->get_one(array('card_sn'=>$user_model_info['s_member_cardid'], 'card_pwd'=>$user_model_info['s_member_cardpwd']));
			//检查是否存在点卡
			if (!$r && $showtip) showmessage(L('pointcard_not_exist'));
			//检查绑定的用户是否同一个人
			if ($r['owner_uid']>0 && $r['owner_uid']!=$user_model_info['userid'] && $showtip) showmessage(L('pointcard_bind_other'));
			//检查卡号是否过期
			if ($r['owner_uid']<=0 && $r['endtime']>0 && $r['endtime']<SYS_TIME && $showtip) showmessage(L('pointcard_expired'));
			//如果以上都没有问题，则绑定
			if ($r['owner_uid']<=0)
			{
				$pc_db->update(array('owner_uid'=>$user_model_info['userid'],), array('pid' => $r['pid']));
			}
		}
		else 
		{
			$user_model_info['s_member_cardid'] = '';
			$user_model_info['s_member_cardpwd'] = '';
		}
		if ($unset_uid == 1) unset($user_model_info['userid']);
		return $user_model_info;
	}
	
	/**
	 * 点卡与会员绑定功能
	 */
	public function pointcard_bind()
	{
		//检查模块是否存在
		if (!module_exists('pointcard')) showmessage(L('pointcard_model_close'));
		$bind_name = trim(strip_tags($_POST['bind_name']));
		$bind_stno = trim(strip_tags($_POST['bind_stno']));
		if (empty($bind_name) || empty($bind_stno)) showmessage(L('pointcard_param_error'));
		$siteid = get_siteid();
		//加载点卡数据库模型
		$pc_db = pc_base::load_model('pointcard_model');
		//获得点卡的相关信息
		$pc_info = $pc_db->get_one(array('card_sn'=>$bind_name, 'card_pwd'=>$bind_stno,'siteid'=>$siteid));
		if (empty($pc_info)) showmessage(L('pointcard_not_exist'));
		//检查点卡是否已绑定
		if ($pc_info['owner_uid']>0) showmessage(L('pointcard_binded'));
		//检查点卡是否已经过期
		if ($pc_info['endtime']>0 && $pc_info['endtime']<SYS_TIME) showmessage(L('pointcard_expired'));
		//开始绑定
		$return = $pc_db->update(array('owner_uid'=>$this->memberinfo['userid']), array('pid'=>$pc_info['pid']));
		if ($return)
		{
			showmessage(L('pointcard_bind_succ'), 'index.php?m=pay&c=deposit&a=pc_pay');
		}
		else 
		{
			showmessage(L('pointcard_bind_fail'));
		}
	}
	
	/**
	 * 会员绑定功能，要设置的需要为：默认模板、申请绑定、申请解除、绑定确认、解除确认
	 */
	public function account_bind ()
	{
		$t = trim(strip_tags($_GET['t']));
		$member_modelid = $this->memberinfo['modelid'];
		if (!in_array($member_modelid, array(10, 12))) showmessage(L('bind_forbidden'));
		
		//申请绑定
		if ($t == 'bind')
		{
			$bind_name = trim(strip_tags($_POST['bind_name']));
//			if (CHARSET=='gbk') {
//				$bind_name = iconv('UTF-8', 'GBK', $bind_name);
//			}
			if ($bind_name == $this->memberinfo['username']) showmessage(L('bind_self'));
			if ($member_modelid==12)
			{//家长身份申请绑定
				$bind_stno = trim(strip_tags($_POST['bind_stno']));
//				if (CHARSET=='gbk') {
//					$bind_stno = iconv('UTF-8', 'GBK', $bind_stno);
//				}
				if (empty($bind_name) && empty($bind_stno)) showmessage(L('bind_param_error'));
				//获得会员信息
				$member_info = $this->db->get_one(array('username'=>$bind_name), 'userid,nickname,modelid,islock,siteid');
				if (empty($member_info)) showmessage(L('bind_data_error'));
				//如果不是学生，跳开
				if ($member_info['modelid']!=10) showmessage(L('bind_student_error'));
				//切换会员模型数据
				$this->db->set_model($member_info['modelid']);
				$model_info = $this->db->get_one(array('s_student_id'=>$bind_stno), 'userid,s_real_name');
				if (empty($model_info)) showmessage(L('bind_data_error'));
				//检查学生是否已经存在绑定信息
				$this->db->set_bind_model(true);
				$bind_infos = $this->db->get_one(array('student_uid'=>$model_info['userid']), 'parent_uid');
				if ($bind_infos) showmessage($bind_infos['parent_uid']==$this->memberinfo['userid']?L('bind_to_your'):L('bind_to_other'));
				if ($member_info['userid']!=$model_info['userid']) showmessage(L('bind_user_not_match'));
				//执行绑定
				$insrt_id = $this->db->insert(array('parent_uid'=>$this->memberinfo['userid'], 'student_uid'=>$member_info['userid'], 'bind_time'=>SYS_TIME, 'status'=>'0'));
				if ($insrt_id)
				{
					$this->message_db = pc_base::load_model('message_model');
					$this->message_db->add_message($bind_name,$this->memberinfo['username'],L('bind_request_msg'),sprintf(L('bind_request_msg_content'),$this->memberinfo['username']),true);
					showmessage(L('bind_request'), 'index.php?m=member&c=index&a=account_bind');
				}
				else 
				{
					showmessage(L('bind_request_fail'));
				}
			}
			else 
			{//学生身份申请绑定
				$bind_name = trim(strip_tags($_POST['bind_name']));
				if (empty($bind_name)) showmessage(L('bind_param_error'));
				//获得会员信息
				$member_info = $this->db->get_one(array('username'=>$bind_name), 'userid,nickname,modelid,islock,siteid');
				if (empty($member_info)) showmessage(L('bind_data_error'));
				//如果不是家长，跳开
				if ($member_info['modelid']!=12) showmessage(L('bind_parent_error'));
				//检查该学生是否已经和家长进行了绑定
				$this->db->set_bind_model(true);
				$bind_infos = $this->db->get_one(array('student_uid'=>$this->memberinfo['userid']), 'parent_uid');
				if ($bind_infos) showmessage(L('bind_exist'));
				//执行绑定
				$insrt_id = $this->db->insert(array('parent_uid'=>$member_info['userid'], 'student_uid'=>$this->memberinfo['userid'], 'bind_time'=>SYS_TIME, 'status'=>'1'));
				if ($insrt_id)
				{
					$this->message_db = pc_base::load_model('message_model');
					$this->message_db->add_message($bind_name,$this->memberinfo['username'],L('bind_request_msg'),sprintf(L('bind_request_msg_content'),$this->memberinfo['username']),true);
					showmessage(L('bind_request'), 'index.php?m=member&c=index&a=account_bind');
				}
				else 
				{
					showmessage(L('bind_request_fail'));
				}
			}
		}
		//同意绑定
		elseif ($t == 'bind_agree')
		{
			$bid = intval($_GET['bid']);
			if ($bid<=0) showmessage(L('bind_param_error'));
			$this->db->set_bind_model(true);
			$bid_info = $this->db->get_one(array('id'=>$bid));
			if (empty($bid_info)) showmessage(L('data_error'));
			//检查状态是否为绑定状态
			if (!in_array($bid_info['status'], array(0,1))) showmessage(L('bind_status_error'));
			//根据会员模型判断权限
			if (!in_array($this->memberinfo['modelid'], array(10,12))) showmessage(L('identity_error'));
			//根据会员模型判断所有者
			if ($this->memberinfo['modelid']==10 && $this->memberinfo['userid']!=$bid_info['student_uid'])
			{//学生模型
				showmessage(L('not_yourself'));
			}
			elseif ($this->memberinfo['modelid']==12 && $this->memberinfo['userid']!=$bid_info['parent_uid'])
			{//家长模型
				showmessage(L('not_yourself'));
			}
			//判断是否是自己发起的请求
			if ($this->memberinfo['modelid']==10 && $bid_info['status']==1)
			{
				showmessage(L('op_yourself_start'));
			}
			elseif ($this->memberinfo['modelid']==12 && $bid_info['status']==0)
			{
				showmessage(L('op_yourself_start'));
			}
			//执行绑定操作
			$return = $this->db->update(array('status'=>'4', 'bind_time'=>SYS_TIME), array('id'=>$bid));
			if ($return)
			{
				$this->db->set_bind_model();//切换回会员表
				$bid_uid = $this->memberinfo['modelid']==10 ? $bid_info['parent_uid'] : $bid_info['student_uid'];
				$bind_member = $this->db->get_one(array('userid'=>$bid_uid), 'username');
				$this->message_db = pc_base::load_model('message_model');
				$this->message_db->add_message($bind_member['username'],$this->memberinfo['username'],L('bind_request_msg'),sprintf(L('agreed_bind_resuest'),$this->memberinfo['username']),true);
				showmessage(L('bind_your_done').L('bind_agree').L('bind_request_msg'), 'index.php?m=member&c=index&a=account_bind');
			}
			else 
			{
				showmessage(L('bind_op_fail'));
			}
		}
		//拒绝绑定
		elseif ($t == 'bind_reject')
		{
			$bid = intval($_GET['bid']);
			if ($bid<=0) showmessage(L('bind_param_error'));
			$this->db->set_bind_model(true);
			$bid_info = $this->db->get_one(array('id'=>$bid));
			if (empty($bid_info)) showmessage(L('data_error'));
			//检查状态是否为绑定状态
			if (!in_array($bid_info['status'], array(0,1))) showmessage(L('bind_status_error'));
			//根据会员模型判断权限
			if (!in_array($this->memberinfo['modelid'], array(10,12))) showmessage(L('identity_error'));
			//根据会员模型判断所有者
			if ($this->memberinfo['modelid']==10 && $this->memberinfo['userid']!=$bid_info['student_uid'])
			{//学生模型
				showmessage(L('not_yourself'));
			}
			elseif ($this->memberinfo['modelid']==12 && $this->memberinfo['userid']!=$bid_info['parent_uid'])
			{//家长模型
				showmessage(L('not_yourself'));
			}
			//判断是否是自己发起的请求
			if ($this->memberinfo['modelid']==10 && $bid_info['status']==1)
			{
				showmessage(L('op_yourself_start'));
			}
			elseif ($this->memberinfo['modelid']==12 && $bid_info['status']==0)
			{
				showmessage(L('op_yourself_start'));
			}
			//执行拒绝操作
			$return = $this->db->delete(array('id'=>$bid));
			if ($return)
			{
				$this->db->set_bind_model();//切换回会员表
				$bid_uid = $this->memberinfo['modelid']==10 ? $bid_info['parent_uid'] : $bid_info['student_uid'];
				$bind_member = $this->db->get_one(array('userid'=>$bid_uid), 'username');
				$this->message_db = pc_base::load_model('message_model');
				$this->message_db->add_message($bind_member['username'],$this->memberinfo['username'],L('bind_request_msg'),sprintf(L('rejected_bind_resuest'),$this->memberinfo['username']),true);
				showmessage(L('bind_your_done').L('bind_reject').L('bind_request_msg'), 'index.php?m=member&c=index&a=account_bind');
			}
			else 
			{
				showmessage(L('bind_op_fail'));
			}
		}
		//申请解除
		elseif ($t == 'unbind')
		{
			$bid = intval($_GET['bid']);
			if ($bid<=0) showmessage(L('bind_param_error'));
			$this->db->set_bind_model(true);
			$bid_info = $this->db->get_one(array('id'=>$bid, 'status'=>'4'));
			if (empty($bid_info)) showmessage(L('data_error'));
			//根据会员模型判断权限
			if (!in_array($this->memberinfo['modelid'], array(10,12))) showmessage(L('identity_error'));
			//根据会员模型判断所有者
			if ($this->memberinfo['modelid']==10 && $this->memberinfo['userid']!=$bid_info['student_uid'])
			{//学生模型
				showmessage(L('not_yourself'));
			}
			elseif ($this->memberinfo['modelid']==12 && $this->memberinfo['userid']!=$bid_info['parent_uid'])
			{//家长模型
				showmessage(L('not_yourself'));
			}
			//发送解除状态
			$status_num = $this->memberinfo['modelid']==10 ? 3 : 2;
			$return = $this->db->update(array('status'=>$status_num, 'bind_time'=>SYS_TIME), array('id'=>$bid));
			if ($return)
			{
				$this->db->set_bind_model();//切换回会员表
				$bid_uid = $this->memberinfo['modelid']==10 ? $bid_info['parent_uid'] : $bid_info['student_uid'];
				$bind_member = $this->db->get_one(array('userid'=>$bid_uid), 'username');
				$this->message_db = pc_base::load_model('message_model');
				$this->message_db->add_message($bind_member['username'],$this->memberinfo['username'],L('unbind_request_msg'),sprintf(L('unbind_request_msg_content'),$this->memberinfo['username']),true);
				showmessage(L('bind_request'), 'index.php?m=member&c=index&a=account_bind');
			}
			else 
			{
				showmessage(L('bind_request_fail'));
			}
		}
		//同意解除
		elseif ($t == 'unbind_agree')
		{
			$bid = intval($_GET['bid']);
			if ($bid<=0) showmessage(L('bind_param_error'));
			$this->db->set_bind_model(true);
			$bid_info = $this->db->get_one(array('id'=>$bid));
			if (empty($bid_info)) showmessage(L('data_error'));
			//检查状态是否为绑定状态
			if (!in_array($bid_info['status'], array(2,3))) showmessage(L('bind_status_error'));
			//根据会员模型判断权限
			if (!in_array($this->memberinfo['modelid'], array(10,12))) showmessage(L('identity_error'));
			//根据会员模型判断所有者
			if ($this->memberinfo['modelid']==10 && $this->memberinfo['userid']!=$bid_info['student_uid'])
			{//学生模型
				showmessage(L('not_yourself'));
			}
			elseif ($this->memberinfo['modelid']==12 && $this->memberinfo['userid']!=$bid_info['parent_uid'])
			{//家长模型
				showmessage(L('not_yourself'));
			}
			//判断是否是自己发起的请求
			if ($this->memberinfo['modelid']==10 && $bid_info['status']==3)
			{
				showmessage(L('op_yourself_start'));
			}
			elseif ($this->memberinfo['modelid']==12 && $bid_info['status']==2)
			{
				showmessage(L('op_yourself_start'));
			}
			//同意解除绑定，执行删除
			$return = $this->db->delete(array('id'=>$bid));
			if ($return)
			{
				$this->db->set_bind_model();//切换回会员表
				$bid_uid = $this->memberinfo['modelid']==10 ? $bid_info['parent_uid'] : $bid_info['student_uid'];
				$bind_member = $this->db->get_one(array('userid'=>$bid_uid), 'username');
				$this->message_db = pc_base::load_model('message_model');
				$this->message_db->add_message($bind_member['username'],$this->memberinfo['username'],L('unbind_request_msg'),sprintf(L('agreed_unbind_resuest'),$this->memberinfo['username']),true);
				showmessage(L('bind_your_done').L('bind_agree').L('unbind_request_msg'), 'index.php?m=member&c=index&a=account_bind');
			}
			else 
			{
				showmessage(L('bind_op_fail'));
			}
		}
		//拒绝解除
		elseif ($t == 'unbind_reject')
		{
			$bid = intval($_GET['bid']);
			if ($bid<=0) showmessage(L('bind_param_error'));
			$this->db->set_bind_model(true);
			$bid_info = $this->db->get_one(array('id'=>$bid));
			if (empty($bid_info)) showmessage(L('data_error'));
			//检查状态是否为绑定状态
			if (!in_array($bid_info['status'], array(2,3))) showmessage(L('bind_status_error'));
			//根据会员模型判断权限
			if (!in_array($this->memberinfo['modelid'], array(10,12))) showmessage(L('identity_error'));
			//根据会员模型判断所有者
			if ($this->memberinfo['modelid']==10 && $this->memberinfo['userid']!=$bid_info['student_uid'])
			{//学生模型
				showmessage(L('not_yourself'));
			}
			elseif ($this->memberinfo['modelid']==12 && $this->memberinfo['userid']!=$bid_info['parent_uid'])
			{//家长模型
				showmessage(L('not_yourself'));
			}
			//判断是否是自己发起的请求
			if ($this->memberinfo['modelid']==10 && $bid_info['status']==3)
			{
				showmessage(L('op_yourself_start'));
			}
			elseif ($this->memberinfo['modelid']==12 && $bid_info['status']==2)
			{
				showmessage(L('op_yourself_start'));
			}
			//拒绝解除绑定，重新设置为绑定
			$return = $this->db->update(array('status'=>'4', 'bind_time'=>SYS_TIME), array('id'=>$bid));
			if ($return)
			{
				$this->db->set_bind_model();//切换回会员表
				$bid_uid = $this->memberinfo['modelid']==10 ? $bid_info['parent_uid'] : $bid_info['student_uid'];
				$bind_member = $this->db->get_one(array('userid'=>$bid_uid), 'username');
				$this->message_db = pc_base::load_model('message_model');
				$this->message_db->add_message($bind_member['username'],$this->memberinfo['username'],L('unbind_request_msg'),sprintf(L('rejected_unbind_resuest'),$this->memberinfo['username']),true);
				showmessage(L('bind_your_done').L('bind_reject').L('unbind_request_msg'), 'index.php?m=member&c=index&a=account_bind');
			}
			else 
			{
				showmessage(L('bind_op_fail'));
			}
		}
		//默认
		else 
		{
			//获得已经绑定的信息列表
			$field_name = $member_modelid==10 ? 'student_uid' : 'parent_uid';
			$this->db->set_bind_model(true);
			$bind_list = $this->db->listinfo(array($field_name=>$this->memberinfo['userid']), 'id DESC', 1, 500);
			if ($bind_list)
			{				
				foreach ($bind_list as $bkey => $bval)
				{
					$this->db->set_bind_model();
					if ($member_modelid==10)
					{//学生身份绑定的
						$user_name = $this->db->get_one(array('userid'=>$bval['parent_uid']), 'username, nickname');
						$bind_list[$bkey]['username'] = $user_name['username'];
						$bind_list[$bkey]['student_no'] = "-----";
					}
					else 
					{//家长身份的绑定
						$user_name = $this->db->get_one(array('userid'=>$bval['student_uid']), 'username, nickname');
						$bind_list[$bkey]['username'] = $user_name['username'];
						$this->db->set_model(10);
						$student_no = $this->db->get_one(array('userid'=>$bval['student_uid']), 's_student_id');
						$bind_list[$bkey]['student_no'] = $student_no['s_student_id'];
					}
					$bind_list[$bkey]['format_bind_time'] = date("Y-m-d H:i:s", $bval['bind_time']);
					$bind_list[$bkey]['bind_status_text'] = $this->switch_bind_status($bval['status']);
				}
			}
			include template('member', 'member_bind');
		}		
	}
	
	/**
	 * 切换绑定状态
	 */
	private function switch_bind_status($status = 0)
	{
		$status_text = L('bind_succ');
		switch ($status)
		{
			case 0:
				$status_text = L('bind_parent').L('bind_apply');
				break;
			case 1:
				$status_text = L('bind_student').L('bind_apply');
				break;
			case 2:
				$status_text = L('bind_parent').L('unbind_apply');
				break;
			case 3:
				$status_text = L('bind_student').L('unbind_apply');
				break;
			case 4:
				break;
			default:
				break;
		}
		return $status_text;
	}
}

?>